package server.business.login.handler;

import java.util.ArrayList;
import java.util.List;

import server.ServerHandler;
import server.commonData.IdFactory;
import server.commonData.SqlStatementEnum;
import server.commonData.User;
import server.messageId.S2CMessageId;
import server.msgBean.LoginMessage.CreateAccountReq;
import server.msgBean.LoginMessage.CreateAccountRes;
import server.utils.AbsAsyncQueryDb;
import server.utils.AbsAsyncUpdateDb;

import com.core.NetChannel;
import com.core.initialize.Message;
import com.google.protobuf.GeneratedMessage;

/**
 * 创建用户
 * @author King
 *
 */
public class CreateAccountHandler extends ServerHandler
{

	@Override
	public Class<? extends GeneratedMessage> initBodyClass()
	{
		return CreateAccountReq.class;
	}

	@Override
	protected void handler(Message msg, User user) throws Throwable
	{
		CreateAccountReq req = msg.getBody();
		List<String> params = new ArrayList<String>();
		params.add(req.getAccount());
		new QueryAcoount(SqlStatementEnum.QUERY_USERBYACCOUNT, params, msg.getChannel(), req.getAccount(), req.getPassword());
	}

	/***
	 * 异步查询数据库
	 * @author King
	 *
	 */
	class QueryAcoount extends AbsAsyncQueryDb
	{
		private NetChannel channel;
		private String account;
		private String password;
		
		@SuppressWarnings("rawtypes")
		public QueryAcoount(String sql, List params,NetChannel channel,String account,String password) throws Exception
		{
			super(sql, params);
			this.channel = channel;
			this.account = account;
			this.password = password;
			query(sql, params);
			doCallBack();
		}

		@SuppressWarnings({ "rawtypes", "unchecked" })
		@Override
		protected void queryBack()
		{
			//在查询的过程中连接关闭了
			if(!channel.getChannel().isWritable())
				return;
			//帐号已存在
			if(!this.result.isEmpty())
			{
				Message msg = Message.newMessage();
				msg.setModuleAction(S2CMessageId.Login.MODULE_ID, S2CMessageId.Login.CREATE_ACCOUNT_RES);
				CreateAccountRes.Builder builder = CreateAccountRes.newBuilder();
				builder.setResult(1);
				msg.setBody(builder);
				this.channel.write(msg);
				return;
			}
			//去创建帐号
			List params = new ArrayList();
			params.add(IdFactory.createUserId());
			params.add(this.account);
			params.add(1);
			params.add("囧囧"+System.currentTimeMillis());
			new InsertBack(SqlStatementEnum.REPLACE_USER, params, this.channel, this.account, this.password);
		}
	}
	
	class InsertBack extends AbsAsyncUpdateDb
	{
		private NetChannel channel;
		private String account;
		private String password;
		public InsertBack(String sql, List params,NetChannel channel,String account,String password)
		{
			super(sql, params);
			this.channel = channel;
			this.account = account;
			this.password = password;
		}

		@Override
		protected void updateBack()
		{
			//在查询的过程中连接关闭了
			if(!channel.getChannel().isWritable())
				return;
			Message msg = Message.newMessage();
			CreateAccountRes.Builder builder = CreateAccountRes.newBuilder();
			builder.setResult(0);
			msg.setBody(builder);
			this.channel.write(msg);
		}
	}
}
